<?php

namespace app\index\controller;
use think\Db;
use \think\Cookie;
use \think\Model;
use think\Session;
class User extends Model
{
    protected $createTime = 'regtime';
    protected $updateTime = 'lasttime';
}
class Bind extends \think\Controller
{
    public function bind()   //绑定QQ
    {
        $data['qq_sid'] = Session::get('qq_sid');
        $data['nickname'] = Session::get('nickname');
        $data['gender'] = Session::get('gender');
        $data['local'] = Session::get('local');
        $data['birth_year'] = Session::get('birth_year');
        //获取请求变量
        if (input('param.')) {
            //输入数据
            $in_put = input('param.');
            $search = new User();
            $search_data = $search->whereOr('name', $in_put['name'])->whereOr('phone', $in_put['phone'])->whereOr('qq', $in_put['qq'])->whereOr('qq_sid', $data['qq_sid'])->find();
            if ($search_data) {
                $this->error('用户存在了哦');
            } else {
                if (mb_strwidth($in_put['password']) < 6) {
                    $this->error('密码太短了哦，请输入大于6位的密码');
                }
                $ylnuser = new User;
                $ylnuser->data([
                    'name' => $in_put['name'],
                    'mail' => $in_put['mail'],
                    'password' => md5($in_put['password']),
                    'phone' => $in_put['phone'],
                    'qq' => $in_put['qq'],
                    'city' => $data['local'],
                    'birth' => $data['birth_year'] . '-01-01',
                    'reminder' => 0,
                    'status' => 1,
                    'safecode' => '00000000000000000000',
                    'qq_sid' => $data['qq_sid']
                ]);
                if ($ylnuser->save()) {
                    $this->success('注册成功，正在前往登录页面', '../../index/Login/login');
                }
            }
        }
        //获取数据库
        $webdate = Db::name('index')->getById(1);//根据用户识别码
        //渲染模板
        $this->assign('name', $webdate['name']);
        $this->assign('copy', $webdate['copy']);
        return $this->fetch('index/bind', [], ['__PUBLIC__' => '/public/static/home']);
    }
    public function bind_exist()    // 绑定已有账号
    {
        $qq_sid = Session::get('qq_sid');
        if (input('post.')) {
            $in_put = input('post.');
            $search = new User();
            $search_data = $search->where('name', $in_put['name'])->where('password', md5($in_put['password']))->find();
            if (!empty($search_data)) {
                $randcode = random(20, 'all');
                $user = User::get($search_data['Id']);
                $user->safecode = $randcode;
                $user->qq_sid = $qq_sid;
                $user->save();
                cookie(['prefix' => '', 'expire' => 36000]);
                cookie('safecode', $randcode, '36000');
                $this->success('登陆成功哦', '../../home/home/home');
            } else {
                $this->error('用户名或者密码不对哦');
            }
        }
        //获取数据库
        $webdate = Db::name('index')->getById(1);//根据用户识别码
        //渲染模板
        $this->assign('name', $webdate['name']);
        $this->assign('copy', $webdate['copy']);
        return $this->fetch('index/bind_exist', [], ['__PUBLIC__' => '/public/static/home']);
    }
}
